package com.drinks.yiniang.adapter.category;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseMultiItemQuickRCVAdapter;
import com.drinks.yiniang.entity.category.CategoryGoodsJson;
import com.drinks.yiniang.entity.category.CategoryHotBrandJson;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.ui.widget.textview.TagTextView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

import static com.drinks.yiniang.entity.category.CategoryGoodsJson.ITEM_TYPE_CATEGORY_GOODS;
import static com.drinks.yiniang.entity.category.CategoryGoodsJson.ITEM_TYPE_CATEGORY_HOT_GOODS;

/**
 * 分类商品数据adapter
 * @author han
 * @date 2019/8/30
 */
public class CategoryGoodsAdapter extends BaseMultiItemQuickRCVAdapter<CategoryGoodsJson.CategoryGoodsList, BaseViewHolder> {

    private TagTextView mTagTextView;

    private StringBuilder mBuilder;

    public CategoryGoodsAdapter(Context mContext, @Nullable List<CategoryGoodsJson.CategoryGoodsList> data) {
        super(data);

        addItemType(ITEM_TYPE_CATEGORY_GOODS, R.layout.item_category_goods);
        //addItemType(ITEM_TYPE_CATEGORY_HOT_TITLE, R.layout.item_category_hot_brand_title);
        addItemType(ITEM_TYPE_CATEGORY_HOT_GOODS, R.layout.item_category_hot_goods);

        mBuilder = new StringBuilder();
    }

    @Override
    protected void convert(BaseViewHolder helper, CategoryGoodsJson.CategoryGoodsList item) {
        switch (helper.getItemViewType()) {
            case ITEM_TYPE_CATEGORY_GOODS://商品列表
                GlideUtils.loadSquareImage(mContext, (RatioRoundImageView) helper.getView(R.id.iv_category_goods_cover), item.getCover_url());
                //名称
                helper.setText(R.id.tv_category_goods_name, item.getName());
                //推荐语
                helper.setText(R.id.tv_category_goods_slogan, item.getRecommend_reason());
                //价格
                mBuilder = new StringBuilder("¥");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getApp_price()));
                helper.setText(R.id.tv_category_goods_price, mBuilder);

                //特价
                helper.setGone(R.id.tv_category_goods_special_sale, StringUtils.judgeGreaterNumber(item.getActivity_price(), 0f));

                //活动tag
                addLabelTag((AutoLineFeedLayout) helper.getView(R.id.ll_category_goods_act_label), item.getActivity());

                helper.addOnClickListener(R.id.iv_category_goods_add_cart);
                break;
            case ITEM_TYPE_CATEGORY_HOT_GOODS://热门品牌
                if(item.getTempHotBrand() == null) {
                    item.setTempHotBrand(new CategoryHotBrandJson.CategoryHotBrandList());
                }

                //logo
                GlideUtils.loadSquareImage(mContext, (RatioRoundImageView) helper.getView(R.id.iv_category_hot_goods_cover), item.getTempHotBrand().getLogo());
                //name
                helper.setText(R.id.tv_category_hot_goods_name, item.getTempHotBrand().getName());
                break;
            default:
                break;
        }
    }

    private void addLabelTag(AutoLineFeedLayout layout, List<GoodsJson.GoodsActivity> actList) {
        layout.removeAllViews();

        if (CommonUtils.isEmptyList(actList)) {
            layout.setVisibility(View.GONE);

            return;
        }

        layout.setVisibility(View.VISIBLE);

        for (GoodsJson.GoodsActivity data : actList) {
            mTagTextView = new TagTextView(mContext);

            mTagTextView.setTagColor(data.getTag_color());
            mTagTextView.setText(data.getTag_name());

            layout.addView(mTagTextView);
        }
    }
}
